<?php

namespace App\Http\Controllers\Bap;

use App\Http\Controllers\Controller;
use App\Models\Autonym;
use App\Models\User;
use DB;
use Exception;
use Illuminate\Http\Request;

/**
 * 用户中心
 */
class CenterController extends Controller
{
    /**
     * 用户中心-基本信息
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function center_info(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid')) {
            //错误提示
            return respF(1003);
        }
        //用户id
        $uid = $request->get('uid');
        //查找用户
        $user = User::find($uid);
        //未找到用户
        if (!$user) {
            //错误提示
            return respF(1001);
        }
        //账号 邮箱或者手机
        $data['account'] = $user->email ?? $user->phone;
        //昵称
        $data['nickname'] = $user->account;
        //是否设置密码
        $data['password'] = isset($user->password);
        //是否设置交易密码
        $data['pay_pass'] = isset($user->pay_pass);
        //登录历史
        $log = $user->login_log()->first();
        //最后登录时间
        $data['last_login_time'] = $log->created_at->format('Y-m-d H:i:s');
        //最后登录ip
        $data['last_login_ip'] = $log->ip;

        return response()->json(['status' => 1, 'msg' => '成功', 'data' => $data]);
    }

    /**
     * 用户中心-登录历史
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login_history(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid')) {
            //错误提示
            return respF(1003);
        }
        //用户id
        $uid = $request->get('uid');
        //查找用户
        $user = User::find($uid);
        //用户不存在
        if (!$user) {
            //错误提示
            return respF(1001);
        }
        //登录历史（3条）
        $data = $user->login_log()->limit(3)->get();
        //返回数据
        return response()->json(['status' => 1, 'msg' => '成功', 'data' => $data]);
    }

    /**
     * 保存实名认证信息提交
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function save_autonym(Request $request)
    {
        try {
            //验证必传参数
            if (!$request->has('uid', 'name', 'indentity_card','time','hand_card', 'front_card', 'back_card')) {
                //缺少参数
                throw new Exception(1000);
            }
            //用户ID
            $uid = $request->get('uid');
            //真实姓名
            $name = $request->get('name');
            //身份证号码
            $indentity_card = $request->get('indentity_card');
            //正面照片
            $front_card = $request->get('front_card');
            //背面照片
            $back_card = $request->get('back_card');
            $time = $request->get('time');
            $hand_card = $request->get('hand_card');
            //验证是否为空
            if (empty($uid) || empty($name) || empty($indentity_card) || empty($front_card) || empty($back_card) || empty($time) || empty($hand_card)) {
                //参数错误
                throw new Exception(1003);
            }
            if (!isIdentity($indentity_card)) {

                return respF(2027);
            }
            //查找用户
            $user = User::find($uid);
            //用户不存在
            if (!$user) {
                //抛出错误
                throw new Exception(1001);
            }
            //验证身份证号码唯一
            $card = Autonym::where(['indentity_card' => $indentity_card, 'status' => 2])->first();
            //身份证号码已注册
            if ($card) {
                //抛出
                throw new Exception(3017);
            }
            if(strpos($front_card,'//')){
                $img_s = parse_url($front_card);
                $front_card = ltrim($img_s['path'],'/');
            }
            if(strpos($back_card,'//')){
                $img_s = parse_url($back_card);
                $back_card = ltrim($img_s['path'],'/');
            }
            if(strpos($hand_card,'//')){
                $img_s = parse_url($hand_card);
                $hand_card = ltrim($img_s['path'],'/');
            }
            //查找实名信息
            $check = Autonym::where('uid', $uid)->first();
            //开始事务
            DB::beginTransaction();
            if (!$check) {
                //写入实名表
                Autonym::create([
                    'uid'            => $uid,
                    'name'           => $name,
                    'indentity_card' => $indentity_card,
                    'front_card'     => $front_card,
                    'back_card'      => $back_card,
                    'hand_card'      => $hand_card,
                    'time'           => $time,
                    'status'         => 1,
                ]);
                //修改用户状态
                $user->update(['real_status' => 3]);
            } else if($check->status == 3) {
                //更新实名表
                $check->update([
                    'name'           => $name,
                    'indentity_card' => $indentity_card,
                    'front_card'     => $front_card,
                    'back_card'      => $back_card,
                    'hand_card'      => $hand_card,
                    'time'           => $time,
                    'status'         => 1,
                ]);
                //修改用户状态
                $user->update(['real_status' => 3]);
            } else {
                throw new Exception(2028);
            }
            
            //提交事务
            DB::commit();
            //成功提示
            return response()->json(['status' => 1, 'msg' => '成功']);

        } catch (Exception $e) {
            //回滚事务
            DB::rollback();
            //错误号
            $msg = $e->getMessage();

            return respF($msg);
        }
    }
}
